package cn.com.shopec.erp.multiChannel.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.multiChannel.common.MultiChannelConstant;
import cn.com.shopec.erp.multiChannel.service.IJingDongService;
import cn.com.shopec.erp.multiChannel.service.ITaoBaoService;

/**
 * 多渠道对接Controller
 * @author Terry
 *
 */
@Controller
@RequestMapping("/multiChannel")
public class MultiChannelController {
private static final Log log = LogFactory.getLog(MultiChannelController.class);
	
	@Resource
    private IJingDongService jingDongService;
	@Resource
    private ITaoBaoService taoBaoService;
	
	@RequestMapping("/toSyncOrder")
    public String toSyncOrder() {
        return "/multiChannel/syncOrder";
    }
	
	/**
	 * 同步京东订单数据
	 * @return
	 */
	@RequestMapping("/syncJDOrderInfo")
	@ResponseBody
	public String syncJDOrderInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String result=SZSCMConstant.OPERATION_ERROR;
		String startDate=request.getParameter("startDate");
		String endDate=request.getParameter("endDate");
		String syncChannel=request.getParameter("syncChannel");
		log.info("request param startDate = "+startDate+",and endDate = "+endDate+",and syncChannel = "+syncChannel);
		if(startDate!=null && !"".equals(startDate) && endDate!=null && !"".equals(endDate) && syncChannel!=null && !"".equals(syncChannel)){
			switch (syncChannel) {
				//京东渠道
				case MultiChannelConstant.JD_MULTI_CHANNEL:
					result=jingDongService.syncJDOrderInfo(startDate, endDate);
					log.info("response result = "+result);
					break;
				//淘宝渠道
				case MultiChannelConstant.TB_MULTI_CHANNEL:
					result=taoBaoService.syncTBOrderInfo(startDate, endDate, MultiChannelConstant.TB_ORDER_TYPE);
					log.info("response result = "+result);
					break;
				//天猫渠道
				case MultiChannelConstant.TM_MULTI_CHANNEL:
					result=taoBaoService.syncTBOrderInfo(startDate, endDate, MultiChannelConstant.TM_CHANNEL_TYPE);
					log.info("response result = "+result);
					break;
			}
		}
		return result;
	}
	
	/**
	 * 京东授权回调
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/JDCallbackUrl")
	public String JDCallbackUrl(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String returnUrl="/multiChannel/fail";
		//取得京东回调code
		String code=request.getParameter("code");
		log.info("JDCallbackUrl result code is = "+code);
		if(code!=null && !"".equals(code)){
			code = new String(code.getBytes("ISO-8859-1"), "UTF-8");
			boolean flag=jingDongService.getAccessTokenByCode(code);
			log.info("jingDongService.getAccessTokenByCode result flag is = "+flag);
			if(flag){
				returnUrl="/multiChannel/success";
			}
		}
		return returnUrl;
	}
	
	/**
	 * 淘宝授权回调
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/TBCallbackUrl")
	public String TBCallbackUrl(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String returnUrl="/multiChannel/fail";
		//取得京东回调code
		String code=request.getParameter("code");
		log.info("TBCallbackUrl result code is = "+code);
		if(code!=null && !"".equals(code)){
			code = new String(code.getBytes("ISO-8859-1"), "UTF-8");
			boolean flag=taoBaoService.getAccessTokenByCode(code);
			log.info("taoBaoService.getAccessTokenByCode result flag is = "+flag);
			if(flag){
				returnUrl="/multiChannel/success";
			}
		}
		return returnUrl;
	}
}
